package com.css.fxfzfxqh.modules.lifelineEngineering.controller;

import cn.hutool.core.io.FileUtil;
import com.css.fxfzfxqh.base.dict.repository.entity.DictItemEntity;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.common.rest.model.bo.ysx.AttachmentInfoBo;
import com.css.fxfzfxqh.common.rest.model.bo.ysx.UploadShpBo;
import com.css.fxfzfxqh.common.rest.model.vo.ysx.AttachmentInfoVo;
import com.css.fxfzfxqh.common.rest.model.vo.ysx.SmxVersionVo;
import com.css.fxfzfxqh.modules.lifelineEngineering.param.SmxDataParam;
import com.css.fxfzfxqh.modules.lifelineEngineering.param.UploadShpParam;
import com.css.fxfzfxqh.modules.lifelineEngineering.param.VectorDataParam;
import com.css.fxfzfxqh.modules.lifelineEngineering.service.LifelineDataMaintenanceService;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("lifelineDataMaintenance")
@Slf4j
public class LifelineDataMaintenanceController {

    @Autowired
    private LifelineDataMaintenanceService lifelineDataMaintenanceService;

    /**
     * 查询重复数据
     *
     * @param param
     * @return
     */
    @PostMapping("getRepeatData")
    public RestResponse getRepeatData(@RequestBody UploadShpParam param) {
        try {
            List<SmxVersionVo> list = lifelineDataMaintenanceService.getRepeatData(param);
            return RestResponse.succeed(list);
        } catch (Exception e) {
            log.error("post getRepeatData error:{}", e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 数据版本
     *
     * @return
     */
    @GetMapping("getVersionCode")
    public RestResponse getVersionCode(UploadShpParam param) {
        try {
            Map<String, Object> data = lifelineDataMaintenanceService.getVersionCode(param);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("get getVersionCode error:{}", e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 根据版本号查询
     *
     * @return
     */
    @GetMapping("getByTypeAndVersionId")
    public RestResponse getByTypeAndVersionId(SmxDataParam param) {
        try {
            Map<String, Object> data = lifelineDataMaintenanceService.getByTypeAndVersionId(param);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("get page error:{}", e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 根据版本号删除  删除整个版本
     */
    @GetMapping("deleteByTypeAndVersionId")
    public RestResponse deleteByTypeAndVersionId(String versionId) {
        try {
            Integer code = lifelineDataMaintenanceService.deleteByTypeAndVersionId(versionId);
            if (code != 200) {
                return RestResponse.fail("删除失败!");
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            log.error("get deleteByTypeAndVersionId error:{}", e.getMessage(), e);
            return RestResponse.fail("删除失败!");
        }
    }

    /**
     * 根据类型和id删除  （删除单条数据  可批量）
     *
     * @param type
     * @param ids
     * @return
     */
    @GetMapping("deleteByTypeAndId")
    public RestResponse deleteByTypeAndId(Integer type, String ids) {
        try {
            Integer code = lifelineDataMaintenanceService.deleteByTypeAndId(type, ids);
            if (code != 200) {
                return RestResponse.fail("删除失败!");
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            log.error("get deleteByTypeAndId error:{}", e.getMessage(), e);
            return RestResponse.fail("删除失败!");
        }
    }

    /**
     * 根据id修改
     */
    @PostMapping("updateById")
    public RestResponse updateById(@RequestBody HashMap<Integer, Object> map) {
        try {
            Integer code = lifelineDataMaintenanceService.updateById(map);
            if (code != 200) {
                return RestResponse.fail("修改失败!");
            }
            return RestResponse.succeed("修改成功!");
        } catch (Exception e) {
            log.error("post updateById error:{}", e.getMessage(), e);
            return RestResponse.fail("修改失败!");
        }
    }

    /**
     * 上传shp
     *
     * @param param
     * @return
     */
    @PostMapping("/uploadShp")
    public RestResponse uploadShp(@RequestBody UploadShpParam param) {
        RestResponse response = null;
        try {
            UploadShpBo uploadShpBo = lifelineDataMaintenanceService.uploadShp(param);
            if (uploadShpBo.getCode() == 200) {
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            } else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(uploadShpBo.getMessage());
            }
        } catch (Exception e) {
            String errormessage = "上传失败!";
            response = RestResponse.fail(errormessage);
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 文件上传
     */
    @PostMapping("uploadFile")
    public RestResponse uploadFile(List<MultipartFile> file) {
        try {
            List<AttachmentInfoVo> list = lifelineDataMaintenanceService.uploadFile(file);
            return RestResponse.succeed(list);
        } catch (Exception e) {
            log.error("post uploadFile error:{}", e.getMessage(), e);
            return RestResponse.fail("文件上传失败!");
        }
    }

    /**
     * 获取字典项
     */
    @GetMapping("getByCode")
    public RestResponse getByCode(String code) {
        try {
            List<DictItemEntity> list = lifelineDataMaintenanceService.getByCode(code);
            return RestResponse.succeed(list);
        } catch (Exception e) {
            log.error("get getByCode error:{}", e.getMessage(), e);
            return RestResponse.fail("获取字典项失败!");
        }
    }

    /**
     * 导入枢纽建筑数据
     *
     * @param file
     * @param type
     * @param province
     * @param city
     * @param county
     * @param cover
     * @param id
     * @return
     */
    @PostMapping("importHubBuilding")
    public RestResponse importHubBuilding(@RequestParam("file") MultipartFile file,
                                          @RequestParam("type") Integer type,
                                          @RequestParam("province") String province,
                                          @RequestParam("city") String city,
                                          @RequestParam("county") String county,
                                          @RequestParam("cover") Integer cover,
                                          @RequestParam("id") String id) {
        try {
            if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
                return RestResponse.fail("文件格式不支持!");
            }
            Integer code = lifelineDataMaintenanceService.importHubBuilding(file, type, province, city, county, cover, id);
            if (code == 200) {
                return RestResponse.succeed("导入成功!");
            } else {
                return RestResponse.fail("导入失败!");
            }
        } catch (Exception e) {
            log.error("post importHubBuilding error:{}", e.getMessage(), e);
            return RestResponse.fail("导入失败!");
        }
    }

    /**
     * 上传矢量数据
     */
    @PostMapping("uploadVectorData")
    public RestResponse uploadVectorData(@RequestBody VectorDataParam param) {
        RestResponse response = null;
        try {
            //先查询这个版本下是否有矢量数据，如果有不允许上传
            if (param.getCover() == 0) {
                int count = lifelineDataMaintenanceService.getDataByVersion(param.getVersionId(), param.getType());
                if (count != 0) {
                    return RestResponse.succeed(count);
                }
            }
            Map<String, Object> map = lifelineDataMaintenanceService.uploadVectorData(param);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            if (res) {
                response = RestResponse.succeed(map.get("mess").toString());
            } else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        } catch (Exception e) {
            log.error("post uploadVectorData error:{}", e.getMessage(), e);
            response = RestResponse.fail("上传失败");
        }
        return response;
    }
}
